package com.glorysoft.knowlespda.ui.engineering.view

import android.os.Bundle
import android.widget.TextView
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.afollestad.materialdialogs.lifecycle.lifecycleOwner
import com.glorysoft.knowlespda.BR
import com.glorysoft.knowlespda.R
import com.glorysoft.knowlespda.base.BaseActivity
import com.glorysoft.knowlespda.databinding.ActivityEngineeringBinding
import com.glorysoft.knowlespda.entity.ui.ToastMessageBean
import com.glorysoft.knowlespda.ui.engineering.viewmodel.EngineeringViewModel
import com.glorysoft.knowlespda.utils.FreeApi_DialogUtils
import com.glorysoft.knowlespda.utils.MyUtils
import kotlinx.android.synthetic.main.activity_engineering.*
import kotlinx.android.synthetic.main.activity_engineering.etMachineID

/**
 * FileName: EngineeringActivity
 * Author: XJH
 * Date: 2022/8/5 15:56
 * Description:
 * History:
 */
class EngineeringActivity : BaseActivity<EngineeringViewModel,ActivityEngineeringBinding>() {
    override fun layoutId(): Int {
        return R.layout.activity_engineering
    }

    override fun initVariableId(): Int {
        return BR.engineeringViewModel
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(this.getString(R.string.reset))
        etMachineID.requestFocus()
        MyUtils.setEditTextInput(etMachineID, this)
        tvSubmit.setOnClickListener{
            if (etMachineID.text.toString().isEmpty()){
                viewModel.defUI.toastEvent.value = ToastMessageBean(this.getString(R.string.magazinenohint),false)
            }else{
                showDialogs(etMachineID.text.toString()).show()
            }
        }
    }

    private fun showDialogs(toString: String) : MaterialDialog {
        val dialog =  MaterialDialog(this)
            .cancelable(false)
            .customView(R.layout.dialog_confirm_cancel, noVerticalPadding = true)
            .lifecycleOwner(this)
        val contentView = dialog.getCustomView()
        val tvConfirm: TextView = contentView.findViewById(R.id.tvConfirm)
        val tvCancel: TextView = contentView.findViewById(R.id.tvCancel)

        tvCancel.setOnClickListener {
            dialog.dismiss()
        }


        tvConfirm.setOnClickListener {
            dialog.dismiss()
            viewModel.submit(toString)
        }

        return dialog
    }

    override fun initData() {
        viewModel.defUI.startActivity.observe(this){
            val tipDialog = FreeApi_DialogUtils.getConfirmDialog(this, this.getString(R.string.confirm), this.getString(R.string.operation_succeeded),this.getString(R.string.tips)) {
                finish()
            }
            tipDialog.show(this.supportFragmentManager, "DIALOG")
        }
    }
}